<?php

/*
 * Copyright by Sygma Media Inovasi Ω2013
 * 
 * Lead Programmer : Sony K Wibisono,S.Si,M.T.I
 * E-mail : sony@sonywibisono.com
 * 
 *  
 * 
 */

global $is_login;

function _add(){
    echo "<form action=\"?mod=admin&file=groups\" method=\"post\">    
    <input type=\"hidden\" name=\"action\" value=\"add_process\"/>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td class=\"field_name\">Id</td><td class=\"field_object\">#</td></tr>
<tr><td class=\"field_name\">Group Name</td><td class=\"field_object\"><input type=\"text\" name=\"group_name\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Group Desc</td><td class=\"field_object\"><input type=\"text\" name=\"group_desc\" class=\"textbox\"/> </td></tr>
<tr><td></td><td><input class=\"button\" type=\"submit\" value=\"Kirim\"/>
        <input class=\"button\" type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
}
function _edit(){
    global $db,$id;
    if (!empty($id)){
        $sql="select * from groups where id=$id";
        $row=$db->fetch_row($sql);
        if (!empty($row)){
            extract($row);
            echo "<form action=\"?mod=admin&file=groups\" method=\"post\">    
    <input type=\"hidden\" name=\"action\" value=\"edit_process\"/>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td class=\"field_name\">Id</td><td class=\"field_object\">
<input type=\"hidden\" name=\"id\" value=\"$id\"/># </td></tr>
<tr><td class=\"field_name\">Group Name</td><td class=\"field_object\">
<input type=\"text\" name=\"group_name\" class=\"textbox\" value='$group_name'/> </td></tr>
<tr><td class=\"field_name\">Group Desc</td><td class=\"field_object\">
<textarea cols='40' rows='5' name=\"group_desc\" class=\"textbox\">$group_desc</textarea> </td></tr>
<tr><td></td><td><input class=\"button\" type=\"submit\" value=\"Kirim\"/>
        <input class=\"button\" type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
        }
    }
}
function _add_process(){
    global $db,$id,$group_name,$group_desc;
    if (!empty($group_name)){
        $sql="insert into groups values (null,'$group_name','$group_desc')";
        $db->query($sql);
        refresh_to("?mod=admin&file=groups", "Back to Group Management", 1);
    }
}
function _edit_process(){
    global $db,$id,$group_name,$group_desc;
    if (!empty($group_name)){
        $sql="update groups set group_name='$group_name',group_desc='$group_desc' where id=$id";
        $db->query($sql);
        refresh_to("?mod=admin&file=groups", "Back to Group Management", 1);
    }
}
function _delete(){
    global $db,$id;
    if (!empty($id)){
        $sql="delete from groups";
        $db->query($sql);
        refresh_to("?mod=admin&file=groups", "Back to Group Management", 1);
    }
}
function get_max_page(){
    global $db,$pagesize;
    $sql="select count(*) from groups";
    $row=$db->fetch_row($sql);
    if (!empty($row)){
        return floor($row[0]/$pagesize)+1;
    } else return 0;
}
function _default(){
    global $db,$page,$pagesize,$mod,$file;
    if (empty($page)){
        $start=0;
        $page=0;
    } else {
        $start=$page*$pagesize;
    }
    $sql="select * from groups limit $start,$pagesize";
    $result=$db->query($sql);
    $mp=  get_max_page();
    
    echo "<h2>Group List</h2>";
    echo "<table width='100%' cellspacing=0 cellpadding=2>";
    echo "<tr><td>";
    for($i=0;$i<$mp;$i++){
        $pname=$i+1;
        echo "<a href=\"?mod=$mod&file=$file&page=$i\">$pname</a> |";
    }
    echo "<a href=\"?mod=$mod&file=$file&action=add\">Add New</a></td></tr>";
    echo "<tr><th>Id</th><th>Group Name</th><th>Group Desc</th><th>Action</th></tr>";
    while ($row=$db->fetch_array($result)){
        extract($row);
        echo "<tr><td>$id</td><td>$group_name</td><td>$group_desc</td><td><a href=\"?mod=$mod&file=$file&action=edit&id=$id\">
        <img class=\"icon_small\" src='img/edit.png' title='Edit'/></a>
        <a href=\"?mod=$mod&file=$file&action=delete&id=$id\"><img class=\"icon_small\" src='img/delete.png' title='Delete'/></a></td></tr>";
    }
    echo "<tr><td>";
    for($i=0;$i<$mp;$i++){
        $pname=$i+1;
        echo "<a href=\"?mod=$mod&file=$file&page=$i\">$pname</a> |";
    }
    echo "</tr>";
    echo "</table>";
}
global $action;
if (!isset($is_login)){
    refresh_to("?mod=admin", "You Must be login first...", 2);
} else {
show_menus('header');
echo "<div class=\"sub_content\">";
switch($action){
    case 'add':
        _add();
        break;
    case 'add_process':
        _add_process();
        break;
    case 'edit':
        _edit();
        break;
    case 'edit_process':
        _edit_process();
        break;
    case 'delete':
        _delete();
        break;
    default :
        _default();
}
echo "</div>";
}
?>
